package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.StudentScore;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IStudentScoreService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.StudentInfor;
import com.ruoyi.system.service.IStudentInforService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-27
 */
@Service
public class StudentInforServiceImpl implements IStudentInforService 
{
    @Autowired
    private StudentInforMapper studentInforMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private IStudentScoreService studentScoreService;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    ISysDeptService deptService;
    /**
     * 查询【请填写功能名称】
     * 
     * @param studentIdCard 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public StudentInfor selectStudentInforByStudentIdCard(String studentIdCard)
    {
        return studentInforMapper.selectStudentInforByStudentIdCard(studentIdCard);
    }

    @Override
    public StudentInfor getSchoolOpensInfoByIdCard(String studentIdCard) {
        return studentInforMapper.getSchoolOpensInfoByIdCard(studentIdCard);

    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param studentInfor 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<StudentInfor> selectStudentInforList(StudentInfor studentInfor)
    {
        return studentInforMapper.selectStudentInforList(studentInfor);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param studentInfor 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertStudentInfor(StudentInfor studentInfor)
    {
        studentInfor.setCreateTime(DateUtils.getNowDate());
        return studentInforMapper.insertStudentInfor(studentInfor);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param studentInfor 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateStudentInfor(StudentInfor studentInfor)
    {
        studentInfor.setUpdateTime(DateUtils.getNowDate());
        SysUser sysUser=new SysUser();
        sysUser.setBirthday(studentInfor.getBirthday());
        sysUser.setSex(studentInfor.getSex());
        sysUser.setPhonenumber(studentInfor.getPhonenumber());
        sysUser.setUserName(studentInfor.getStudentIdCard());
        sysUser.setNickName(studentInfor.getStudentName());

        userService.updateUserByUserName(sysUser);
        return studentInforMapper.updateStudentInfor(studentInfor);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param studentIdCards 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteStudentInforByStudentIdCards(String[] studentIdCards)
    {
        return studentInforMapper.deleteStudentInforByStudentIdCards(studentIdCards);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param studentIdCard 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteStudentInforByStudentIdCard(String studentIdCard)
    {
        return studentInforMapper.deleteStudentInforByStudentIdCard(studentIdCard);
    }
    List<StudentInfor> addRemark(List<StudentInfor> resultList,String remark,StudentInfor studentInfor){
        studentInfor.setRemake(remark);
        resultList.add(studentInfor);
        return resultList;
    }
    @Autowired
    private SysRoleMapper roleMapper;
    @Override
    public List<StudentInfor> importData(List<StudentInfor> userList) {
        if(userList==null||userList.size()==0){
            return null;
        }
        SysRole role=new SysRole();
        role.setRoleName("学生");
        List<SysRole> sysRoles= roleMapper.selectRoleListByName(role);
        Long [] ids={Long.valueOf(0)};
        if(sysRoles!=null &&sysRoles.size()!=0){
            role=sysRoles.get(0);
            ids[0]=role.getRoleId();
        }
        List<StudentInfor> resultList=new ArrayList<>();
        for (StudentInfor studentInfor:userList
             ) {
            studentInfor.setStudentIdCard(studentInfor.getStudentIdCard().replace("'",""));
            SysUser studentUser=new SysUser();
            studentUser.setRoleIds(ids);
            //查身份证
            if(studentInfor.getStudentIdCard()==null||"".equals(studentInfor.getStudentIdCard())){
                resultList=addRemark(resultList,"身份证号不能为空",studentInfor);
                continue;
            }
            //查学院
            if(studentInfor.getStudentCollege()==null||"".equals(studentInfor.getStudentCollege())){
                resultList=addRemark(resultList,"所属学院不能为空",studentInfor);
                continue;
            }
            //查年级
            if(studentInfor.getStudentGrade()==null||"".equals(studentInfor.getStudentGrade())){
                resultList=addRemark(resultList,"年级不能为空",studentInfor);
                continue;
            }
            //查专业
            if(studentInfor.getStudentMajor()==null||"".equals(studentInfor.getStudentMajor())){
                resultList=addRemark(resultList,"所属专业不能为空",studentInfor);
                continue;
            }
            //查班级
            if(studentInfor.getStudentClass()==null||"".equals(studentInfor.getStudentClass())){
                resultList=addRemark(resultList,"班级不能为空",studentInfor);
                continue;
            }
            //查身份证是否已存在
            SysUser sysUser= userMapper.selectUserByUserName(studentInfor.getStudentIdCard());
            if(sysUser!=null){
                resultList=addRemark(resultList,"系统已存在该身份证号",studentInfor);
                continue;
            }
            //查学院是否已存在
            SysDept sysDept=new SysDept();
            sysDept.setDeptName(studentInfor.getStudentCollege());
            sysDept.setParentId(100L);
            List<SysDept>sysDeptList= deptMapper.selectDeptListByDeptNameAndParent(sysDept);
            SysDept mySysDept=new SysDept();
            if(sysDeptList==null||sysDeptList.size()==0){
//                resultList=addRemark(resultList,"所属学院不存在，请到系统添加学院后再次导入",studentInfor);
//                continue;
                //不存在则插入一条学院的数据
                sysDept.setDeptName(studentInfor.getStudentCollege());
                sysDept.setOrderNum(1);
                mySysDept= deptService.newinsertDept(sysDept);
            }
//            if(sysDeptList.size()>1){
//                resultList=addRemark(resultList,"存在两个相同的学院，请删除其中一个",studentInfor);
//                continue;
//            }

            //查年级是否已存在
            sysDept.setDeptName(studentInfor.getStudentGrade());
            sysDept.setParentId(sysDeptList==null||sysDeptList.size()==0?mySysDept.getDeptId():sysDeptList.get(0).getDeptId());
            sysDeptList= deptMapper.selectDeptListByDeptNameAndParent(sysDept);
            if(sysDeptList==null||sysDeptList.size()==0){
//                resultList=addRemark(resultList,"所属年级不存在，请添加该年级后再次导入",studentInfor);
//                continue;
                //不存在则插入一条年级的数据
                sysDept.setDeptId(null);
                sysDept.setDeptName(studentInfor.getStudentGrade());
                sysDept.setOrderNum(1);
                mySysDept= deptService.newinsertDept(sysDept);
            }
//            if(sysDeptList.size()>1){
//                resultList=addRemark(resultList,"学院："+studentInfor.getStudentCollege()+"存在两个相同的年级，请删除其中一个",studentInfor);
//                continue;
//            }

            //查专业是否已存在
            sysDept.setDeptName(studentInfor.getStudentMajor());
            sysDept.setParentId(sysDeptList==null||sysDeptList.size()==0?mySysDept.getDeptId():sysDeptList.get(0).getDeptId());
            sysDeptList= deptMapper.selectDeptListByDeptNameAndParent(sysDept);
            if(sysDeptList==null||sysDeptList.size()==0){
//                resultList=addRemark(resultList,"所属专业不存在，请到系统添加该专业后再次导入",studentInfor);
//                continue;
                //不存在则插入一条专业的数据
                sysDept.setDeptId(null);
                sysDept.setDeptName(studentInfor.getStudentMajor());
                sysDept.setOrderNum(1);
                mySysDept= deptService.newinsertDept(sysDept);
            }
//            if(sysDeptList.size()>1){
//                resultList=addRemark(resultList,"年级："+studentInfor.getStudentGrade()+"存在两个相同的专业，请删除其中一个",studentInfor);
//                continue;
//            }

            //查班级是否已存在
            sysDept.setDeptName(studentInfor.getStudentClass());
            sysDept.setParentId(sysDeptList==null||sysDeptList.size()==0?mySysDept.getDeptId():sysDeptList.get(0).getDeptId());
            sysDeptList= deptMapper.selectDeptListByDeptNameAndParent(sysDept);
            if(sysDeptList==null||sysDeptList.size()==0){
//                resultList=addRemark(resultList,"所属班级不存在，请到系统添加该班级后再次导入",studentInfor);
//                continue;
                //不存在则插入一条班级的数据
                sysDept.setDeptId(null);
                sysDept.setDeptName(studentInfor.getStudentClass());
                sysDept.setOrderNum(1);
                mySysDept= deptService.newinsertDept(sysDept);
            }
//            if(sysDeptList.size()>1){
//                resultList=addRemark(resultList,"存在两个相同的班级，请删除其中一个",studentInfor);
//                continue;
//            }

            studentUser.setUserName(studentInfor.getStudentIdCard());
            studentUser.setNickName(studentInfor.getStudentName());
            studentUser.setDeptId(sysDeptList==null||sysDeptList.size()==0?mySysDept.getDeptId():sysDeptList.get(0).getDeptId());
            studentUser.setPhonenumber(studentInfor.getPhonenumber());
            String sex="";
            if(studentInfor.getSex()==null||"".equals(studentInfor.getSex())){
                sex="2";
            }
            else if(studentInfor.getSex().contains("男")){
                sex="0";
            }
            else {
                sex="1";
            }
            studentUser.setSex(sex);
            studentUser.setUserType("学生");
            //帐号状态（0正常 1停用）
            studentUser.setStatus("0");
            //删除标志（0代表存在 2代表删除）
            studentUser.setDelFlag("0");
            if(StringUtils.isNotEmpty(studentUser.getPhonenumber()) && !userService.checkPhoneUnique(studentUser)){
                resultList=addRemark(resultList,"新增用户'" + studentUser.getUserName() + "'失败，手机号码已存在",studentInfor);
                continue;
            }
            studentUser.setPassword(SecurityUtils.encryptPassword("123456"));
            studentUser.setBirthday(studentInfor.getBirthday());
            userService.insertUser(studentUser);
            if(studentInfor.getStatus()==null||"".equals(studentInfor.getStatus())){
                studentInfor.setStatus("待报道");
            }
            StudentInfor oldStudentInfor =studentInforMapper.selectStudentInforByStudentIdCard(studentInfor.getStudentIdCard());
            if(oldStudentInfor==null){
                studentInforMapper.insertStudentInfor(studentInfor);
            }else {
                studentInforMapper.updateStudentInfor(studentInfor);
            }

            StudentScore studentScore=new StudentScore();
            studentScore.setIdCard(studentInfor.getStudentIdCard());
            studentScore.setSubmissionScore(studentInfor.getSubmissionScore());
            studentScore.setChinese(studentInfor.getChinese());
            studentScore.setMath(studentInfor.getMath());
            studentScore.setLanguage(studentInfor.getLanguage());
            studentScore.setComprehensive(studentInfor.getComprehensive());
            studentScore.setCulturalScore(studentInfor.getCulturalScore());
            studentScore.setMajorScore(studentInfor.getMajorScore());
            studentScore.setProfessionalNum(studentInfor.getProfessionalNum());
            studentScore.setMajorScoreYt(studentInfor.getMajorScoreYt());
            studentScore.setFeatureScore(studentInfor.getFeatureScore());
            studentScore.setPhysics(studentInfor.getPhysics());
            studentScore.setChemistry(studentInfor.getChemistry());
            studentScore.setBiology(studentInfor.getBiology());
            studentScore.setPolitics(studentInfor.getPolitics());
            studentScore.setHistory(studentInfor.getHistory());
            studentScore.setGeography(studentInfor.getGeography());
            StudentScore oldStudentScore= studentScoreService.selectStudentScoreByIdCard(studentScore.getIdCard());
            if(oldStudentScore==null){
                studentScoreService.insertStudentScore(studentScore);
            }else {
                studentScoreService.updateStudentScore(studentScore);
            }
        }
        return resultList;
    }

    @Override
    public List<StudentInfor> newSelectStudentInforList(StudentInfor studentInfor) {
        return studentInforMapper.newSelectStudentInforList(studentInfor);
    }

    @Override
    public int updateStudentStatusByIdCard(StudentInfor infor) {
        StudentInfor studentInfor=new StudentInfor();
        studentInfor.setStudentIdCard(infor.getStudentIdCard());
        studentInfor.setStatus(infor.getStatus());
        return studentInforMapper.updateStudentInfor(studentInfor);

    }

    @Override
    public List<Map<String, Object>> countStudent(StudentInfor studentInfor) {
        return studentInforMapper.countStudent(studentInfor);
    }

    @Override
    public List<StudentInfor> addImportData(List<StudentInfor> userList) {
        for (StudentInfor s :userList
             ) {
            StudentInfor ts =new StudentInfor();
            ts.setStudentIdCard(s.getStudentIdCard());
            ts.setTeacher(s.getTeacher());
            ts.setDormitory(s.getDormitory());
            studentInforMapper.updateStudentInfor(ts);
        }
        return null;
    }


    public static void main(String[] args) {
        System.out.println(SecurityUtils.encryptPassword("Gyrw@123"));
    }
}
